//
//  SSUserOrderFooterView.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/3/15.
//

import UIKit

class SSUserOrderFooterView: UIView, UITextFieldDelegate {
    
    var myCouponClosure: MyCouponClosure?     // 闭包
    var limitCountLabel: UILabel = UILabel()
    var placeholderLabel: UILabel = UILabel()
    let limitCount:Int = 100
    var coupon: SSCoupon = SSCoupon()
    var totalPrice: Float = 0
    

    
    @IBOutlet weak var totalLab: UILabel!
    @IBOutlet weak var textView: UITextView!
    @IBOutlet weak var jianLab: UILabel!
    @IBOutlet weak var newLab: UILabel!
    
    @IBOutlet weak var thresholdFeeLab: UILabel!
    
    @IBOutlet weak var threshsholdDesLab: UILabel!
    
    class func footerView()->SSUserOrderFooterView{
        return Bundle.main.loadNibNamed("SSUserOrderFooterView", owner: nil, options: nil)?.last as! SSUserOrderFooterView
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        coupon.money = "0.0"
        
        textView.delegate = self
        textView.layer.borderWidth = 1
        textView.layer.cornerRadius = 8
        textView.layer.borderColor = UIColor.init(hex: "#4CA6F8")?.cgColor
        textView.isUserInteractionEnabled = true
        
        let placeholderLab = UILabel()
        placeholderLab.textAlignment = .left
        placeholderLab.text = "请输入其他叮嘱"
        placeholderLab.font = UIFont.systemFont(ofSize: 16)
        placeholderLab.frame = CGRect(x: 12, y: 12, width: 150, height: 14)
        placeholderLab.textColor = UIColor.init(hex: "#CCCCCC")
        textView.addSubview(placeholderLab)
        self.placeholderLabel = placeholderLab

        let countLab = UILabel()
//        countLab.backgroundColor = UIColor.red
        countLab.text = "0/\(limitCount)"
        countLab.textAlignment = .right
        countLab.font = UIFont.systemFont(ofSize: 16)
        countLab.frame = CGRect(x: ScreenWidth - 16 - 12 - 16 - 150, y: textView.frame.height - 20 - 12, width: 150, height: 20)
        countLab.textColor = UIColor.init(hex: "#CCCCCC")
        textView.addSubview(countLab)
        self.limitCountLabel = countLab
        
//        setData()
    }
    
    func setData() {

        jianLab.text = "总优惠 $\(coupon.money)"
        let string2 = jianLab.text
        let attributedString2 = NSMutableAttributedString(string:string2 ?? "")
        attributedString2.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.init(hex: "#FC4242")!, range: NSRange(location: 3, length: string2!.count - 3))
        jianLab.attributedText = attributedString2
 
        totalLab.text = "总计 $\(totalPrice - stringToFloat(str: coupon.money))"
        let string = totalLab.text
        let attributedString = NSMutableAttributedString(string:string ?? "")
        attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.init(hex: "#FC4242")!, range: NSRange(location: 2, length: string!.count - 2))
        totalLab.attributedText = attributedString

    }
    
    @IBAction func couponAction(_ sender: Any) {
        let vc = SSMyCouponController.init()
        vc.isUserOrder = true
        vc.closure { number in
            self.coupon = number
            self.myCouponClosure!(number)
//            self.setData()
        }
        UIViewController.current().navigationController?.pushViewController(vc, animated: true)
    }
    
    func couponClosure(_ finished: @escaping MyCouponClosure) {
        myCouponClosure = finished
    }
    
}

///user/getCoopTag
//获取合作标签
    extension SSUserOrderFooterView : UITextViewDelegate {
        public func textViewDidChange(_ textView: UITextView) {
            if textView.text.count > limitCount {
                // 获得已输出字数与正输入字母数
                let selectRange = textView.markedTextRange

                // 获取高亮部分 － 如果有联想词则解包成功
                if let selectRange = selectRange {
                    let position =  textView.position(from: (selectRange.start), offset: 0)
                    if (position != nil) {
                        return
                    }
                }

                let textContent = textView.text ?? ""
                let textNum = textContent.count

                // 截取
                if textNum > limitCount && limitCount > 0 {
                        textView.text = string_prefix(index: limitCount, text: textContent)
                }
            }
                self.limitCountLabel.text =  "\(textView.text.count)/\(limitCount)"
        }

        public func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
                self.placeholderLabel.isHidden = true
            return true
        }

        public func textViewDidEndEditing(_ textView: UITextView) {
            if textView.text.isEmpty {
                    self.placeholderLabel.isHidden = false
            } else {
                    self.placeholderLabel.isHidden = true
            }
        }

        public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
            if textView.returnKeyType == .done {
                if text == "\n" {
                    textView.resignFirstResponder()
                }
                return true
            }
            return true
        }

        // 字符串的截取 从头截取到指定index
        private func string_prefix(index:Int,text:String) -> String {
            if text.count <= index {
                return text
            } else {
                let index = text.index(text.startIndex, offsetBy: index)
                let str = text.prefix(upTo: index)
                return String(str)
            }
        }
}
